package com.zhouyx.userservice.config;

import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.threads.ThreadPoolExecutor;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
public class TomcatThreadConfigListener implements ApplicationListener<WebServerInitializedEvent> {

    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        if (event.getWebServer() instanceof TomcatWebServer) {
            TomcatWebServer tomcatWebServer = (TomcatWebServer) event.getWebServer();
            Connector connector = tomcatWebServer.getTomcat().getConnector();
            ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) connector.getProtocolHandler().getExecutor();

            int maxThreads = threadPoolExecutor.getMaximumPoolSize();
            System.out.println("Tomcat Maximum Threads: " + maxThreads);
        }
    }
}
